package www.bplead.com.controller.action.student;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import www.bplead.com.controller.action.BaseNetAction;
import www.bplead.com.controller.action.student.entity.Class;
import www.bplead.com.controller.action.student.entity.Course;
import www.bplead.com.controller.action.student.entity.Student;
import www.bplead.com.controller.action.student.event.StudentQueryRequest;
import www.bplead.com.controller.action.student.event.StudentQueryResponse;
import www.bplead.com.controller.action.student.event.StudentUpdateRequest;
import www.bplead.com.controller.action.student.event.StudentUpdateResponse;
import www.bplead.com.controller.db.DbConstant;

/**
 * Created by wangyangzi on 2016/10/19.
 */
public class StudentAction extends BaseNetAction{
    private static final String TAG = "StudentAction";
    private Context mContext ;

    @Inject
    public StudentAction(Context context) {
        super(context);
        mContext = context;
    }

    @Subscribe
    public void studentInfoUpdate(StudentUpdateRequest studentUpdateRequest){
        Student stu = studentUpdateRequest.student;
        StudentUpdateResponse response = new StudentUpdateResponse();
        if(stu == null){
            response.result = false;
        }else{
            init();
            ContentValues values = new ContentValues();
            values.put(DbConstant.TableStudent.NAME,stu.getName());
            values.put(DbConstant.TableStudent.NO,stu.getNo());
            values.put(DbConstant.TableStudent.CLASS_ID,stu.get_class().getId());
            if(stu.getCourseList().size() > 0){
                values.put(DbConstant.TableStudent.COURSE_ID,stu.getCourseList().get(0).getId());
            }
            try {
                //存在ID执行修改操作
                if(stu.getId() > 0){
                    values.put(DbConstant.TableStudent._ID,stu.getId());
                    String where = DbConstant.TableStudent._ID + "=?";
                    String[] selectionArgs = new String[]{"" + stu.getId()};
                    mContext.getContentResolver().update(DbConstant.TableStudent.CONTANT_URI,values,where,selectionArgs);
                }
                //不存在ID执行新增操作
                else{
                    Uri stuUri = mContext.getContentResolver().insert(DbConstant.TableStudent.CONTANT_URI,values);
                    int id = Integer.parseInt(ContentUris.parseId(stuUri) + "");
                    stu.setId(id);
                }
                response.student = stu;
                response.result = true;
            }catch (Exception e){
                e.printStackTrace();
                response.result = false;
            }
            EventBus.getDefault().post(response);
        }
    }

    @Subscribe
    public void studentInfoQuery(StudentQueryRequest studentQueryRequest){
        Log.v(TAG,"studentInfoQuery>>true");
        StudentQueryResponse response = new StudentQueryResponse();

        List<Student> studentList = new ArrayList<>();
        Map<Integer,String> claMap = new HashMap<>();
        Map<Integer,String> couMap = new HashMap<>();

        String[] projection = null;
        String selection = null;
        String[] selectionArgs = null;
        String order = null;
        Cursor stuCursor = mContext.getContentResolver().query(DbConstant.TableStudent.CONTANT_URI,projection,selection,selectionArgs,order);
        Cursor classCursor = mContext.getContentResolver().query(DbConstant.TableClass.CONTANT_URI,null,null,null,null);
        Cursor courseCursor = mContext.getContentResolver().query(DbConstant.TableCourse.CONTANT_URI,null,null,null,null);
        if(stuCursor != null && stuCursor.getCount() > 0){
            while (stuCursor.moveToNext()){
                int id = stuCursor.getInt(stuCursor.getColumnIndex(DbConstant.TableStudent._ID));
                String name = stuCursor.getString(stuCursor.getColumnIndex(DbConstant.TableStudent.NAME));
                String no = stuCursor.getString(stuCursor.getColumnIndex(DbConstant.TableStudent.NO));
                int classId = stuCursor.getInt(stuCursor.getColumnIndex(DbConstant.TableStudent.CLASS_ID));
                int courseId = stuCursor.getInt(stuCursor.getColumnIndex(DbConstant.TableStudent.COURSE_ID));

                Class cla = new Class();
                cla.setId(classId);
                Course cou = new Course();
                cou.setId(courseId);
                List<Course> courseList = new ArrayList<>();
                courseList.add(cou);

                Student stu = new Student();
                stu.setId(id);
                stu.setName(name);
                stu.setNo(no);
                stu.set_class(cla);
                stu.setCourseList(courseList);
                studentList.add(stu);
            }
        }
        if(classCursor != null && classCursor.getCount() > 0){
            while (classCursor.moveToNext()) {
                int id = classCursor.getInt(classCursor.getColumnIndex(DbConstant.TableClass._ID));
                String name = classCursor.getString(classCursor.getColumnIndex(DbConstant.TableClass.NAME));
                claMap.put(id,name);
            }
        }
        if(courseCursor != null && courseCursor.getCount() > 0){
            while (courseCursor.moveToNext()) {
                int id = courseCursor.getInt(courseCursor.getColumnIndex(DbConstant.TableCourse._ID));
                String name = courseCursor.getString(courseCursor.getColumnIndex(DbConstant.TableCourse.NAME));
                couMap.put(id,name);
            }
        }
        for(int i=0;i<studentList.size();i++){
            Student stu = studentList.get(i);
            String className = claMap.get(stu.get_class().getId());
            String courseName = couMap.get(stu.getCourseList().get(0).getId());
            stu.get_class().setName(className);
            stu.getCourseList().get(0).setName(courseName);
        }
        response.result = true;
        response.studentList = studentList;
        EventBus.getDefault().post(response);
    }

    private void init(){
        int[] classIds = {1,2,3,4,5};
        String[] classNames = {"高一(1)班","高一(2)班","高一(3)班","高一(4)班","高一(5)班"};
        int[] courseIds = {1,2,3,4,5,6,7};
        String[] courseNames = {"语文","数学","英语","化学","物理","政治","历史"};
        for(int i=0;i<classIds.length;i++){
            ContentValues values = new ContentValues();
            values.put(DbConstant.TableClass.NAME,classNames[i]);
            mContext.getContentResolver().insert(DbConstant.TableClass.CONTANT_URI,values);
        }
        ContentValues[] contentValues = new ContentValues[courseIds.length];
        for(int i=0;i<courseIds.length;i++){
            ContentValues values = new ContentValues();
            values.put(DbConstant.TableCourse.NAME,courseNames[i]);
            contentValues[i] = values;
        }
        mContext.getContentResolver().bulkInsert(DbConstant.TableCourse.CONTANT_URI,contentValues);
    }
}
